﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections;
using eventr.View.Util;

namespace eventr.View
{
    /// <summary>
    /// Base class for all view models in this application
    /// </summary>
    public class ViewModel : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged Members

        protected void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
        
        /// <summary>
        /// Commands are more like EventBrokers actually, probably not the best name for them :)
        /// </summary>
        #region Commands

        static CommandManager commandManager;

        static ViewModel()
        {
            commandManager = new CommandManager();
        }

        public static CommandManager Commands
        {
            get
            {
                return commandManager;
            }
        }

        #endregion
    }
    public class CommandManager : IEnumerable<Command>
    {
        Dictionary<string, Command> commands;

        public CommandManager()
        {
            commands = new Dictionary<string, Command>();
        }

        public Command this[string index]
        {
            get
            {

                if (!commands.ContainsKey(index))
                {
                    commands.Add(index, new Command());
                }

                return commands[index];
            }
        }


        #region IEnumerable<Command> Members

        public IEnumerator<Command> GetEnumerator()
        {
            foreach (Command c in commands.Values)
            {
                yield return c;
            }
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class Command : INotifyPropertyChanged
    {
        bool enabled = true;

        public event EventHandler<DataEventArgs<Object>> Executed;

        public void Execute()
        {
            onExecute(null, null);
        }

        public void Execute(object sender)
        {
            onExecute(sender, null);
        }

        public void Execute(object sender, object param)
        {
            onExecute(sender, param);
        }

        void onExecute(object sender, object param)
        {
            if (Executed != null)
            {
                Executed(sender, new DataEventArgs<object>(param));
            }
        }

        public bool Enabled
        {
            get
            {
                return enabled;
            }
            set
            {
                enabled = value;
                onPropertyChanged("Enabled");
            }
        }


        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected void onPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

        #endregion
    }
}
